﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace iHub
{
    public partial class editAccount : Form
    {
        private AccountManager acctm = new AccountManager();
        private String username = "";
        public editAccount()
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Edit_Closing);
            username = main.form.getloggeduser();
        }

        private void Edit_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                main.form.Enabled = true;
                main.form.acctRefresh();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult msg = MessageBox.Show("Are you sure you want to cancel editing?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (msg == DialogResult.Yes)
            {
                main.form.Enabled = true;
                main.form.acctRefresh();
                this.Dispose();
            }
        }

        private void acctselectnew_Click(object sender, EventArgs e)
        {
            DialogResult msg = MessageBox.Show("Select another account to edit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (msg == DialogResult.Yes)
            {
                userCollection.Text = "";
                accountselect.Enabled = false;
                editdetails.Visible = false;
                adminbutton.Checked = false;
                encoderbutton.Checked = false;
                userselect.Enabled = true;
                acctedit.Visible = false;
                acctselectnew.Enabled = false;
            }
        }

        private void editAccount_Load(object sender, EventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();
            Boolean type = true;

            list = acctm.getUserList(type, username);

            if (adminbutton.Checked != false || encoderbutton.Checked != false)
            {
                accountselect.Enabled = true;
                userselect.Enabled = false;
                acctselectnew.Enabled = true;

                userCollection.Items.Clear();

                for (int i = 0; i < list.Count; i++)
                {
                    userCollection.Items.Add(list[i] as string);
                }
            }
            else
            {
                userselect.Enabled = true;
            }
        }

        private void userCollection_SelectedIndexChanged(object sender, EventArgs e)
        {
            String username = Convert.ToString(userCollection.SelectedItem);
            Account a = new Account();
            Boolean type;
            Boolean status;
            String password;
            String question;
            String answer;

            a = acctm.getUser(username);

            username = a.getUsername();
            password = a.getPass();
            type = a.getUsertype();
            status = a.getStatus();
            question = a.getQuestion();
            answer = a.getAnswer();

            accountselect.Enabled = false;
            editdetails.Visible = true;
            acctedit.Visible = true;

            acctPassword.Text = password;

            if (type)
            {
                acctAdmin.Checked = true;
            }
            else
            {
                acctEncoder.Checked = true;
            }

            acctsecurityquestion.Text = question;
            acctsecurityanswer.Text = answer;

            if (status)
            {
                activebutton.Checked = true;
            }
            else
            {
                inactivebutton.Checked = true;
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void acctedit_Click(object sender, EventArgs e)
        {
            Account n = new Account();

            if (acctPassword.Text != "" || acctsecurityquestion.Text != "" || acctsecurityanswer.Text != "")
            {
                DialogResult msg = MessageBox.Show("Update with selected details?", "Continue", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (msg == DialogResult.Yes)
                {
                    MessageBox.Show("Update Successful", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    n.setUsername(Convert.ToString(userCollection.SelectedItem));
                    n.setPass(acctPassword.Text);
                    n.setQuestion(acctsecurityquestion.Text);
                    n.setAnswer(acctsecurityanswer.Text);

                    if (acctAdmin.Checked)
                    {
                        n.setUsertype(true);
                    }
                    else if (acctEncoder.Checked)
                    {
                        n.setUsertype(false);
                    }

                    if (activebutton.Checked)
                    {
                        n.setStatus(true);
                    }
                    else if (inactivebutton.Checked)
                    {
                        n.setStatus(false);
                    }

                    acctm.editUserAccount(n);

                    userCollection.Text = "";
                    userCollection.Items.Clear();

                    accountselect.Enabled = false;
                    editdetails.Visible = false;
                    adminbutton.Checked = false;
                    encoderbutton.Checked = false;
                    userselect.Enabled = true;

                    acctselectnew.Enabled = false;
                    acctedit.Visible = false;
                    main.form.acctRefresh();
                }
            }
            else
            {
                MessageBox.Show("Please make sure that all fields are not empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();
            Boolean type = false;

            list = acctm.getUserList(type, username);

            if (adminbutton.Checked != false || encoderbutton.Checked != false)
            {
                accountselect.Enabled = true;
                userselect.Enabled = false;
                acctselectnew.Enabled = true;

                userCollection.Items.Clear();

                for (int i = 0; i < list.Count; i++)
                {
                    userCollection.Items.Add(list[i] as string);
                }
            }
            else
            {
                userselect.Enabled = true;
            }
        }


    }
}
